﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MShop.Core.Domain;
using MShop.Core.DataAccess;
using MShop.Web.Models;
 

namespace MShop.Web.Areas.Admin.Controllers
{
    public class ShopUsersAddressController : SecureController
    {
         private readonly ICommonDao<ShopUsersAddress> _Commondao;
         private readonly IAreaGroupDao _AreaGroupDao;
         public ShopUsersAddressController(ICommonDao<ShopUsersAddress> commondao,IAreaGroupDao areaGroupDao)
        {
            _Commondao = commondao;
            _AreaGroupDao=areaGroupDao;
        }
        
        public ActionResult Index(Guid userid)
        {
            return View(_Commondao.Find(x=>x.UserId==userid).OrderByDescending(x=>x.Id));
        }
        public ActionResult Create()
        {
            ViewData["area"] = new SelectedAreaGroupModel(_AreaGroupDao.Find(x => x.ParentId == null));
            return View(new ShopUsersAddress());
        }

        [HttpPost]
        public ActionResult Create(ShopUsersAddress address,Guid userid)
        {
            address.UserId = userid;            
            if (ModelState.IsValid)
            {
                _Commondao.Save(address);
                if (address.IsDefault == true)
                {
                    var q = from item in _Commondao.GetAll() where item.UserId == userid select item;
                    q.ToList().ForEach((x) => { 
                        x.IsDefault = false ;
                        _Commondao.Update(x);
                    });
                }
                _Commondao.SaveChanges();
                return RedirectToAction("index", new { userid = userid });
            }
            ViewData["area"] = new SelectedAreaGroupModel(_AreaGroupDao.Find(x => x.ParentId == null));
            return View(address);
        }

        public ActionResult Edit(int id, Guid userid)
        {
            var model=_Commondao.GetById(id);
            ViewData["area"] = new SelectedAreaGroupModel(_AreaGroupDao.Find(x => x.ParentId == null), model.AreaId,model.AreaGroup);
            return View(model);
        }


        public ActionResult Delete(int id, Guid userid)
        {
            var brand = _Commondao.GetById(id);
            _Commondao.Delete(brand);
            _Commondao.SaveChanges();
            return RedirectToAction("index", new { userid = userid });
        }


        [HttpPost]
        public ActionResult Edit(int id, ShopUsersAddress address, Guid userid)
        {
           
            if (ModelState.IsValid)
            {
                var dbaddress = _Commondao.GetById(id);
                UpdateModel<ShopUsersAddress>(dbaddress, new string[] { "Mobile", "Tel", "AreaId", "ReceiverName", "IsDefault", "Postcode", "Location","Memo" });
                if (address.IsDefault == true)
                {
                    var q = from item in _Commondao.GetAll() where item.UserId == userid && item.Id != dbaddress.Id select item;
                    q.ToList().ForEach((x) =>
                    {
                        x.IsDefault = false;
                        _Commondao.Update(x);
                    });
                }
                _Commondao.SaveChanges();
                return RedirectToAction("index", new { userid = userid });
            }
            ViewData["area"] = new SelectedAreaGroupModel(_AreaGroupDao.Find(x => x.ParentId == null));
            return View(address);

            
        }
    }
}
